/**
 * @author zengfh
 * @version 1.0
 * date 2018/04/27 13:51
 */
public class TestBank {
    
    public static void main(String[] args) {
        Bank bank = new Bank();
        
        Person personA = new Person("A",bank,100);
        Person personB = new Person("B",bank,200);
        
        personA.start();
        personB.start();
    }
    
    
    static class Bank{
        private  int money = 2000;
        
        /**
         * 银行被取出的钱
         * @param getMoney
         */
        public void out(int getMoney){
            while (money >= 0){
                //synchronized (this){
                    if (money >= getMoney){
                        money = money - getMoney;
                        System.out.println(Thread.currentThread().getName()+" 取出"+getMoney+"元,当前余额为: "+money+" 元");
                        
                    } else {
                        System.out.println(Thread.currentThread().getName() + "取钱" + getMoney +"元,余额不足,当前余额为: "+money+" 元");
                        break;
                    }
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                //}
                
            }
        }
    }
    
    static class Person extends Thread{
        
        private Bank bank;
        
        private int money;//每次取出多少钱
        
        public Person(String name,Bank bank,int money){
            super(name);
            this.bank = bank;
            this.money = money;
        }
        
        @Override
        public void run() {
            super.run();
            bank.out(money);
        }
    }
    
}
